package com.shixi.shixitraining.shixitraining.ui.tabhome.activity;

import android.content.Intent;
import android.graphics.drawable.ColorDrawable;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.support.v4.content.ContextCompat;
import android.support.v7.widget.GridLayoutManager;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.OrientationHelper;
import android.support.v7.widget.RecyclerView;
import android.view.View;
import android.view.WindowManager;
import android.widget.ImageView;
import android.widget.PopupWindow;
import android.widget.TextView;

import com.bumptech.glide.Glide;
import com.scwang.smartrefresh.layout.SmartRefreshLayout;
import com.shixi.shixitraining.shixitraining.R;
import com.shixi.shixitraining.shixitraining.base.BaseActivity;
import com.shixi.shixitraining.shixitraining.http.CallbackImple;
import com.shixi.shixitraining.shixitraining.http.HttpUtil;
import com.shixi.shixitraining.shixitraining.test.TestPopup;
import com.shixi.shixitraining.shixitraining.ui.tabhome.adapter.PopupAdapter;
import com.shixi.shixitraining.shixitraining.ui.tabhome.adapter.TrainingListAdapter;
import com.shixi.shixitraining.shixitraining.ui.tabhome.entity.TrainingListBean;
import com.shixi.shixitraining.shixitraining.util.DensityUtil;
import com.shixi.shixitraining.shixitraining.util.TitleUtil;
import com.shixi.shixitraining.shixitraining.util.ToastUtil;
import com.shixi.shixitraining.shixitraining.view.recycleview.RecycleDivider;
import com.shixi.shixitraining.shixitraining.view.recycleview.RecycleItemClickListener;

import java.util.ArrayList;
import java.util.List;

import retrofit2.Call;

public class TrainingProjectActivity extends BaseActivity implements View.OnClickListener {

    private RecyclerView recyclerView;
    private SmartRefreshLayout refreshLayout;
    private TextView tvNodata;
    private TextView mCompositeTv;
    private PopupWindow popupWindow;
    private TextView mTrainingTv;
    private TextView mGroupTv;
    private int screenHeightPixels;
    private int screenWidthPixels;
    private PopupWindow popupTraining;
    private PopupWindow popupGroup;
    private ImageView mSortIv;
    private ImageView mCategoryIv;
    private ImageView mGroupIv;
    private float alpha = 1.0f;
    Handler handler = new Handler(new Handler.Callback() {
        @Override
        public boolean handleMessage(Message message) {
            switch (message.what) {
                case 1:
                    backgroundAlpha((Float) message.obj);
                    break;
            }
            return false;
        }
    });


    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_training_project);
        initTitle();
        initView();
        initData();

    }

    private void initData() {

        HttpUtil.getInstance().getApiInterface().trainingList("http://api.shixigroup.com/training/index").enqueue(new CallbackImple<TrainingListBean>() {
            @Override
            public void onError(Call<TrainingListBean> call, Throwable t) {
                ToastUtil.showShortToast(TrainingProjectActivity.this, t.getMessage());
            }

            @Override
            public void onSuccess(Call<TrainingListBean> call, TrainingListBean response) {

                if (response != null && response.getData() != null) {
                    if (response.getStatus() == 200) {
                        TrainingListBean.DataBean data = response.getData();
                        List<TrainingListBean.DataBean.CountryBean> country = data.getCountry();
                        List<TrainingListBean.DataBean.SortBean> sort = data.getSort();
                        List<TrainingListBean.DataBean.TypeBean> type = data.getType();
                        final List<TrainingListBean.DataBean.ListsBean>  listData = data.getLists();
                        RecyclerView mRcView = (RecyclerView) findViewById(R.id.rcview_training_projct);
                        GridLayoutManager manager = new GridLayoutManager(TrainingProjectActivity.this, 2);
                        mRcView.setLayoutManager(manager);
                        TrainingListAdapter adapter = new TrainingListAdapter(TrainingProjectActivity.this, listData, Glide.with(TrainingProjectActivity.this));
                        mRcView.setAdapter(adapter);

                        mRcView.addOnItemTouchListener(new RecycleItemClickListener(TrainingProjectActivity.this) {
                            @Override
                            public void itemClickListener(View view, int position) {
                                TrainingListBean.DataBean.ListsBean listsBean = listData.get(position);
                                String id = listsBean.getId();
                                Bundle bundle=new Bundle();
                                bundle.putString("id",id);
                                Intent intent=new Intent(TrainingProjectActivity.this, TrainingDetailActivity.class);
                                intent.putExtras(bundle);
                                startActivity(intent);
                            }
                        });
                    } else {
                        ToastUtil.shortToast(response.getMsg());
                    }
                } else {
                    ToastUtil.shortToast("数据为空");
                }

            }
        });

    }


    private void initView() {


//        实训项目
//        List<HomeGrideTrainingBean> data = new ArrayList<>();
//        data.add(new HomeGrideTrainingBean("http://www.shixi.com/data/comads/2018/03/30/1522376843671.jpg", "联合国新兴全球领导人和全球公民培养计划", "2998.00", 1, 0));
//        data.add(new HomeGrideTrainingBean("http://www.shixi.com/data/comads/2018/03/30/1522376843671.jpg", "联合国新兴全球领导人和全球公民培养计划", "2998.00", 1, 0));
//        data.add(new HomeGrideTrainingBean("http://www.shixi.com/data/comads/2018/03/30/1522376843671.jpg", "联合国新兴全球领导人和全球公民培养计划", "2998.00", 1, 0));
//        data.add(new HomeGrideTrainingBean("http://www.shixi.com/data/comads/2018/03/30/1522376843671.jpg", "联合国新兴全球领导人和全球公民培养计划", "2998.00", 1, 0));
//        data.add(new HomeGrideTrainingBean("http://www.shixi.com/data/comads/2018/03/30/1522376843671.jpg", "联合国新兴全球领导人和全球公民培养计划", "2998.00", 1, 0));
//        data.add(new HomeGrideTrainingBean("http://www.shixi.com/data/comads/2018/03/30/1522376843671.jpg", "联合国新兴全球领导人和全球公民培养计划", "2998.00", 1, 0));
//        data.add(new HomeGrideTrainingBean("http://www.shixi.com/data/comads/2018/03/30/1522376843671.jpg", "联合国新兴全球领导人和全球公民培养计划", "2998.00", 1, 0));
//        data.add(new HomeGrideTrainingBean("http://www.shixi.com/data/comads/2018/03/30/1522376843671.jpg", "联合国新兴全球领导人和全球公民培养计划", "2998.00", 1, 0));

//        HomeTrainingGrideAdapter adapter = new HomeTrainingGrideAdapter(this, data, Glide.with(this));
//        mRcView.setAdapter(adapter);
//        mRcView.addOnItemTouchListener(new RecycleItemClickListener(this) {
//            @Override
//            public void itemClickListener(View view, int position) {
//                startActivity(new Intent(TrainingProjectActivity.this, TrainingDetailActivity.class));
//            }
//        });


    }


    private void initTitle() {

        TitleUtil titleUtil = new TitleUtil(findViewById(R.id.top_bar_layout));
        titleUtil.initTitle(4, R.id.top_bar_center_tv, R.id.top_bar_left_img, 0,
                R.drawable.icon_back, 0, "实训项目", "", "");

        ImageView mBackImg = (ImageView) findViewById(R.id.top_bar_left_img);
        mBackImg.setOnClickListener(this);
        mCompositeTv = (TextView) findViewById(R.id.tv_compiste);
        mTrainingTv = (TextView) findViewById(R.id.tv_training);
        mGroupTv = (TextView) findViewById(R.id.tv_group);
        mSortIv = (ImageView) findViewById(R.id.iv_down_sort);
        mCategoryIv = (ImageView) findViewById(R.id.iv_down_category);
        mGroupIv = (ImageView) findViewById(R.id.iv_down_group);
        mCompositeTv.setOnClickListener(this);
        mTrainingTv.setOnClickListener(this);
        mGroupTv.setOnClickListener(this);

    }


    @Override
    public void onClick(View v) {
        switch (v.getId()) {
            case R.id.top_bar_left_img:
                this.finish();
                break;
            case R.id.tv_compiste:
                initCompositePopup();
                delayPopup();
                mTrainingTv.setTextColor(getResources().getColor(R.color.black));
                mGroupTv.setTextColor(getResources().getColor(R.color.black));
                break;
            case R.id.tv_training:
                initTrainingPopup();
                mTrainingTv.setTextColor(getResources().getColor(R.color.basepresscolor));
                mGroupTv.setTextColor(getResources().getColor(R.color.black));
                mCompositeTv.setTextColor(getResources().getColor(R.color.black));
                mCategoryIv.setImageResource(R.drawable.icon_down_p);
                break;
            case R.id.tv_group:
                initGroupPopup();
                mTrainingTv.setTextColor(getResources().getColor(R.color.black));
                mGroupTv.setTextColor(getResources().getColor(R.color.basepresscolor));
                mCompositeTv.setTextColor(getResources().getColor(R.color.black));
                break;

        }
    }

    //国家
    private void initGroupPopup() {
        View mGroupView = getLayoutInflater().inflate(R.layout.item_session, null);
        if (popupGroup == null) {
            popupGroup = new PopupWindow(mGroupView, screenWidthPixels, screenHeightPixels / 2);
            popupGroup.setBackgroundDrawable(new ColorDrawable(0x0000000));
        }
        popupGroup.setFocusable(true);
        popupGroup.setContentView(mGroupView);
        popupGroup.showAsDropDown(mGroupTv);

        RecyclerView mPopup = (RecyclerView) mGroupView.findViewById(R.id.popup_session_list);
        LinearLayoutManager manager = new LinearLayoutManager(this);
        RecycleDivider divider = new RecycleDivider(new ColorDrawable(ContextCompat.getColor(this, R.color.gray)), OrientationHelper.VERTICAL);
        divider.setHeight(this, 0.5f);
        manager.setOrientation(LinearLayoutManager.VERTICAL);
        mPopup.setLayoutManager(manager);
        mPopup.addItemDecoration(divider);
        List<TestPopup> data = new ArrayList<>();

        data.add(new TestPopup("美国"));
        data.add(new TestPopup("英国"));
        data.add(new TestPopup("日本"));
        data.add(new TestPopup("徳国"));
        data.add(new TestPopup("中国"));
        PopupAdapter adapter = new PopupAdapter(this, data);
        mPopup.setAdapter(adapter);
        mPopup.addOnItemTouchListener(new RecycleItemClickListener(this) {
            @Override
            public void itemClickListener(View view, int position) {
                popupGroup.dismiss();
            }
        });

    }

    //实训类别
    private void initTrainingPopup() {

        View mTrainingView = getLayoutInflater().inflate(R.layout.item_industry, null);
        if (popupTraining == null) {
            popupTraining = new PopupWindow(mTrainingView, screenWidthPixels, screenHeightPixels / 4);
            popupTraining.setBackgroundDrawable(new ColorDrawable(0x0000000));
        }
        popupTraining.setFocusable(true);
        popupTraining.setContentView(mTrainingView);
        popupTraining.showAsDropDown(mCompositeTv);

        RecyclerView mPopup = (RecyclerView) mTrainingView.findViewById(R.id.rcview_poppup_composite);
        LinearLayoutManager manager = new LinearLayoutManager(this);
        RecycleDivider divider = new RecycleDivider(new ColorDrawable(ContextCompat.getColor(this, R.color.gray)), OrientationHelper.VERTICAL);
        divider.setHeight(this, 0.5f);
        manager.setOrientation(LinearLayoutManager.VERTICAL);
        mPopup.setLayoutManager(manager);
        mPopup.addItemDecoration(divider);
        List<TestPopup> data = new ArrayList<>();

        data.add(new TestPopup("金融"));
        data.add(new TestPopup("领导力"));

        PopupAdapter adapter = new PopupAdapter(this, data);
        mPopup.setAdapter(adapter);
        mPopup.addOnItemTouchListener(new RecycleItemClickListener(this) {
            @Override
            public void itemClickListener(View view, int position) {
                popupTraining.dismiss();
            }
        });


    }

    //综合排序
    private void initCompositePopup() {
        View mContentView = getLayoutInflater().inflate(R.layout.item_composite, null);
        if (popupWindow == null) {
            screenHeightPixels = DensityUtil.getScreenHeightPixels(this);
            screenWidthPixels = DensityUtil.getScreenWidthPixels(this);
            popupWindow = new PopupWindow(mContentView, screenWidthPixels, screenHeightPixels / 4);
            popupWindow.setBackgroundDrawable(new ColorDrawable(0x0000000));


        }
        popupWindow.setFocusable(true);
        popupWindow.setContentView(mContentView);
        popupWindow.showAsDropDown(mCompositeTv);

        RecyclerView mPopup = (RecyclerView) mContentView.findViewById(R.id.rcview_poppup_composite);
        LinearLayoutManager manager = new LinearLayoutManager(this);
        RecycleDivider divider = new RecycleDivider(new ColorDrawable(ContextCompat.getColor(this, R.color.gray)), OrientationHelper.VERTICAL);
        divider.setHeight(this, 0.5f);
        manager.setOrientation(LinearLayoutManager.VERTICAL);
        mPopup.setLayoutManager(manager);
        mPopup.addItemDecoration(divider);
        List<TestPopup> data = new ArrayList<>();
        data.add(new TestPopup("综合排序"));
        data.add(new TestPopup("项目时间"));
        data.add(new TestPopup("价格降序"));
        data.add(new TestPopup("价格升序"));

        PopupAdapter adapter = new PopupAdapter(this, data);
        mPopup.setAdapter(adapter);
        mPopup.addOnItemTouchListener(new RecycleItemClickListener(this) {
            @Override
            public void itemClickListener(View view, int position) {
                popupWindow.dismiss();
            }
        });

        popupWindow.setOnDismissListener(new PopupWindow.OnDismissListener() {
            @Override
            public void onDismiss() {
                new Thread(new Runnable() {
                    @Override
                    public void run() {
                        while (alpha < 1.0f) {
                            Message message = handler.obtainMessage();
                            message.what = 1;
                            alpha += 0.01f;
                            message.obj = alpha;
                            handler.sendMessage(message);
                        }
                    }
                }).start();
            }
        });

        backgroundAlpha(1);
    }

    private void delayPopup() {
        new Thread(new Runnable() {
            @Override
            public void run() {
                while (alpha > 0.5f) {
                    Message message = handler.obtainMessage();
                    message.what = 1;
                    alpha -= 0.01f;
                    message.obj = alpha;
                    handler.sendMessage(message);
                }
            }
        }).start();
    }

    //设置popupwindown的背景颜色
    private void backgroundAlpha(float bgAlpha) {

        WindowManager.LayoutParams lp = getWindow().getAttributes();
        lp.alpha = bgAlpha;
        getWindow().setAttributes(lp);
        getWindow().addFlags(WindowManager.LayoutParams.FLAG_DIM_BEHIND);

    }
}